package com.cmii.sjw.department.control.jsc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cmii.sjw.department.control.jsc.mapper.UserMapper;
import com.cmii.sjw.department.control.jsc.model.User;
import com.cmii.sjw.department.control.jsc.model.dto.UserQueryDTO;
import com.cmii.sjw.department.control.jsc.model.entity.Menu;
import com.cmii.sjw.department.control.jsc.model.entity.Role;
import com.cmii.sjw.department.control.jsc.service.IUserService;
import com.cmii.sjw.department.control.jsc.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户 业务层处理
 *
 * @author jfh
 */
@Service
public class UserServiceImpl implements IUserService
{

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserRoleService userRoleService;


    /**
     * 根据条件分页查询用户列表
     *
     * @param user 用户信息
     * @return 用户信息集合信息
     */
    @Override
    public List<User> selectUserList(UserQueryDTO user)
    {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.lambda()
                .eq(user.getUserId() != null, User::getUserId, user.getUserId())
                .like(user.getUserName() != null, User::getUserName, user.getUserName())
                .like(user.getNickName() != null, User::getNickName, user.getNickName())
                .like(user.getEmail()!= null, User::getEmail, user.getEmail())
                .like(user.getPhonenumber()!= null, User::getPhonenumber, user.getPhonenumber())
                .eq(user.getSex()!= null, User::getSex, user.getSex())
                .eq(user.getStatus()!= null, User::getStatus, user.getStatus())
                .eq(user.getIsAuthorized()!= null, User::getIsAuthorized, user.getIsAuthorized())
                .eq(user.getUserType() != null, User::getUserType, user.getUserType());
        return userMapper.selectList(userQueryWrapper);
    }


    @Override
    public List<Role> getUserRoles(Long userId) {
        return userRoleService.getRolesByUserId(userId);
    }

    @Override
    public List<Menu> getUserMenus(Long userId) {
        return userRoleService.getMenusByUserId(userId);
    }

    @Override
    public Long count(LambdaQueryWrapper<User> in) {
        if (in != null) {
            return userMapper.selectCount(in);
        }
        return 0L;
    }

    @Override
    public User getById(long userId) {
        //根据用户ID从数据库中查询
        return userMapper.selectOne(new LambdaQueryWrapper<User>()
                .eq(User::getUserId, userId)
                .eq(User::getStatus, 0));
    }

    /**
     *
     * @author jfh
     * @Date 2025/5/27 14:31
     * @param user  用户信息
     * @return java.lang.String
     */
    @Override
    public String updateById(User user) {
        return userMapper.updateById(user) > 0 ? "更新成功" : "更新失败";
    }


}
